import axios from 'axios'
// import Cookies from 'js-cookie'
import Qs from 'qs'

// import router from '../../router/index'

// import {
//     Loading,
//     Message
// } from 'element-ui'

// let loadingInstance = null
// Cookies.set('pc-data.token','ae703802-74ae-462c-afec-669c5bca024d')
// const token = Cookies.get('SJ-tjjq');
export let baseURL="http://121.37.7.206:7891"
export let uploadURL="http://38.113.192.99:7892"
// export let baseURL="http://38.113.192.99:7891"
// export let baseURL="http://127.0.0.1:7891"

if(process.env.NODE_ENV === 'development') { //开发环境
      // baseURL="http://127.0.0.1:7891"
    // baseURL="http://www.shgb.work:7891"
    baseURL="http://121.37.7.206:7891/"

}
// create an axios instance
const service = axios.create({
    baseURL: baseURL, // url = base url + request url process.env.VUE_APP_BASE_API
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 1000 * 30 // request timeout
})


// let httpCode = { // 这里我简单列出一些常见的http状态码信息，可以自己去调整配置
//     400: '请求参数错误',
//     401: '权限不足, 请重新登录',
//     403: '服务器拒绝本次访问',
//     404: '请求资源未找到',
//     500: '内部服务器错误',
//     501: '服务器不支持该请求中使用的方法',
//     502: '网关错误',
//     504: '网关超时'
// }
service.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

// request interceptor
service.interceptors.request.use(

    config => {
        // config.headers['token'] = token

        // loadingInstance = Loading.service({ // 发起请求时加载全局loading，请求失败或有响应时会关闭
        //     spinner: 'el-icon-loading',
        //     text: '拼命加载中...'
        // })
        // 导出文件的接口， 因为返回的是二进制流， 所以需要设置请求响应类型为blob
        if (config.url.includes('pur/contract/export')) {
            config.headers['responseType'] = 'blob'
        }
        // 文件上传的接口，发送的是二进制流，设置请求头的'Content-Type'
        if (config.url.includes('pur/contract/upload')) {
            config.headers['Content-Type'] = 'multipart/form-data'
        }
        // 统一设置post请求头
        const token = window.sessionStorage.getItem('token')
        config.headers['token'] = token
        // do something before request is sent
        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
        // loadingInstance.close()
        const res = response.data
        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 200 && !res) {
            // Message({
            //     message: res.message || 'Error',
            //     type: 'error'
            // })
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            return res
        }
    },
    error => {
        // loadingInstance.close()
        // if (!token) {
        //     Message({
        //         message: '登录失效，请重新登录',
        //         type: 'error'
        //     })
        //     return Promise.reject(new Error('登录失效，请重新登录'))
        // }
        if (error.response) {
            // let tips = error.response.status in httpCode ? httpCode[error.response.status] : error.response.data.message
            // Message({
            //     message: tips,
            //     type: 'error'
            // })
            if (error.response.status === 401) {
                window.open('http://15.216.22.23:8110/cacent/');
            }
        } else {
            // Message({
            //     message: '请求超时, 请刷新重试',
            //     type: 'error'
            // })
            return Promise.reject(new Error('请求超时, 请刷新重试'))
        }
        return Promise.reject(error)
    }
)

export const post = (url, data, config = {}) => {
    return service({
        method: 'post',
        url,
        data,
        ...config
    }).then(response => {
        return Promise.resolve(response)
    }).catch(error => {
        return Promise.reject(error)
    })
}

export const get = (url, data, config = {}) => {
    let symbol = ''
    if(data){
        if(url.indexOf('?') > -1){
            symbol = ''
        }else{
            symbol = '?'
        }
    }
    url = url+symbol+Qs.stringify(data, {encode: false})
    return service({
        method: 'get',
        url,
        // params: data,
        ...config
    }).then(response => {
        return Promise.resolve(response)
    }).catch(error => {
        return Promise.reject(error)
    })
}
export const getBlob = (url, data, ) => {
    let symbol = '?'
    if(url.indexOf('?') > -1){
        symbol = ''
    }else{
        symbol = '?'
    }
    url = baseURL + url+symbol+Qs.stringify(data, {encode: false})
    window.open(url)
}
